<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div id="app">
    <ul>
      <li v-for="cate in cates" :key="cate.id">
        <img :src="cate.icon" alt="">
        <h5>{{cate.name}}</h5>
      </li>
    </ul>
    <ul>
      <li v-for="item in items" :key="item.id">
        <h4>{{item.name}}</h4>
        <img :src="item.pic" alt="">
      </li>
    </ul>
  </div>
  <script src="./vue.js"></script>
  <script src="./axios.js"></script>
  <script>
    const catePath = "/shop/goods/category/all";
    const itemPath = "/shop/goods/list/v2";
    axios.defaults.baseURL = 'https://api.it120.cc/conner';
    axios.interceptors.request.use(function (config) {
      // 在请求之前 可以在这里做点事情 
      console.log('开始发送请求');
      config.headers.token = 'dwhifgi423rfed';
      return config; // 放行 没有return请求发送不出去
    }, function (error) {
      // 请求出错的拦截 
      return Promise.reject(error);
    });

    // 添加响应拦截
    axios.interceptors.response.use(function (response) {
      // 在响应之前做一些东西 判断token是否过期
      console.log('结果回来了');
      return response;
    }, function (error) {
      // 响应失败 http状态码不是 2xx开头
      return Promise.reject(error);
    })
    const vm = new Vue({
      el: '#app',
      data: {
        cates: [],
        items: []
      },
      created() {
        this.fetchCates();
        this.fetchItems();
      },
      methods: {
        fetchCates() {
          axios.get(catePath, {
            params: {
              a: 10
            }
          }).then(res => {
            if (res.data.code === 0) {
              this.cates = res.data.data
            }
          })
        },
        fetchItems() {
          axios({
            url: itemPath,
            method: 'post',
            data: {
              page: 1,
              pageSize: 10
            }
          }).then(res => {
            if (res.data.code === 0) {
              this.items = res.data.data.result
            }
          })
        }
      }
    })
  </script>
</body>

</html>